using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class Comment
    {

        internal static string currTableName = WebConfig.TablePrefix + "comment";












        public CommentInfo GetCommentInfo(int id)
        {
            CommentInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new CommentInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.channel_id = int.Parse(dr["channel_id"].ToString());
                    model.username = dr["username"].ToString();
                    model.ip = dr["ip"].ToString();
                    model.locked = dr["locked"].ToString()=="1";
                    model.title = dr["title"].ToString();
                    model.content = dr["content"].ToString();
                    model.addtime = DateTime.Parse(dr["addtime"].ToString());
                    model.article_id = int.Parse(dr["article_id"].ToString());
                    model.address = dr["address"].ToString();
                    model.tip = int.Parse(dr["tip"].ToString());
                    model.layer = int.Parse(dr["layer"].ToString());
                }
                dr.Close();
            }
            return model;
        }







        private int GetArticleId(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 article_id from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            object obj = DbHelper.GetSingle(strSql.ToString());
            return (obj != null) ? int.Parse(obj.ToString()) : 0;
        }






        public bool Add(CommentInfo model)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + "(");
            strSql.Append("sys_model_id,channel_id,username,ip,locked,title,content,addtime,article_id,address,tip,layer");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(model.sys_model_id.ToString() + ",");
            strSql.Append(model.channel_id.ToString() + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.username) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.ip) + "',");
            strSql.Append((model.locked ? "1" : "0") + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.content) + "',");
            strSql.Append(WebConfig.IsAccess ? "Now()," : "getdate(),");
            strSql.Append(model.article_id.ToString() + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.address) + "',");
            strSql.Append(model.tip.ToString() + ",");
            strSql.Append(model.layer.ToString());
            strSql.Append(")");

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public bool UpdateContent(int id,int layer,string content)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("content='" + PlugNT.Safe.StringHelper.SqlFilter(content) + "',layer=" + layer.ToString());
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }
        public bool UpdateTip(int id,int tip)
        {
            
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("tip=" + tip.ToString());
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }
        public bool UpdateLocked(int id, bool locked)
        {
            
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("locked=" + (locked ? "1" : "0"));
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool AddTip(int id)
        {
            
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("tip=tip+1");
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }













        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }


        public DataTable GetIdList(int sysModelId, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id] from [" + currTableName + "] where sys_model_id= " + sysModelId);
            strSql.Append(andWhere);

            return DbHelper.GetDataTable(strSql.ToString());
        }







        public DataTable GetList(int sysModelId, string andWhere)
        {
            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  [" + currTableName + "] where sys_model_id= "+ sysModelId);
            strSql.Append(andWhere);
            return DbHelper.GetDataTable(strSql.ToString());
        }

       public DataTable GetList(int sysModelId, int topCount, string andWhere)
       {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + (topCount > 0 ? "top " + topCount : "") + " * from  [" + currTableName + "] where sys_model_id= " + sysModelId);
            strSql.Append(andWhere);
            return DbHelper.GetDataTable(strSql.ToString());
        }


        public DataTable GetList(int sysModelId, int pageSize, int currPage, string andWhere, bool isReverse)
        {
            string sql = "";
            if (isReverse)
            {
                sql = Sql.GetRePagingString(pageSize, currPage, "[" + currTableName + "]", "id", " and sys_model_id=" + sysModelId + andWhere);
            }
            else
            {
                sql = Sql.GetPagingString(pageSize, currPage, "[" + currTableName + "]", "id", " and sys_model_id=" + sysModelId + andWhere);
            }

            return DbHelper.GetDataTable(sql);
        }


        public DataTable GetList(int sysModelId, int pageSize, int currPage, string columns, string andWhere, LeftJoinInfo joininfo)
        {





            
            string sql = Sql.GetRePagingString(pageSize, currPage, currTableName, "id", columns, " and sys_model_id=" + sysModelId +  andWhere, joininfo);
            return DbHelper.GetDataTable(sql);
        }







































        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id= "+ sysModelId + andWhere;
            object obj = DbHelper.GetSingle(sql);
            return obj == null ? -1 : int.Parse(obj.ToString());
        }

        public int GetTipCount(int sysModelId, string andWhere)
        {
            string sql = "select tip from [" + currTableName + "] where sys_model_id= "+ sysModelId + andWhere;
            object obj= DbHelper.GetSingle(sql);
            return obj == null ? -1 : int.Parse(obj.ToString());
        }


    }
}

